﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InSafetyAirClient.GUI.Flight
{
    public partial class CreateF : UserControl
    {
        InSafetyAirService.Flight f= null;
        List<InSafetyAirService.FlightSegment> segments = new List<InSafetyAirService.FlightSegment>();
        List<InSafetyAirService.Schedule> shedules = new List<InSafetyAirService.Schedule>();
        DateTime sDate = new DateTime();
        DateTime eDate = new DateTime();
        List<InSafetyAirClient.InSafetyAirService.Airport> airports = null;
        List<InSafetyAirClient.InSafetyAirService.Plane> planes = null;
        List<InSafetyAirClient.InSafetyAirService.AirLine> airlines = null;
        DataTable dataTable1 = new DataTable();
        DataTable dataTable2 = new DataTable();
        AutoCompleteStringCollection autoComplete = new AutoCompleteStringCollection();
        private bool hus = true;

        public CreateF()
        {
            InitializeComponent();
        }

        private void CreateF_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                populateList();
            }
        }

        private void populateList()
        {
            dataTable1.Columns.Add("Id", typeof(int));
            dataTable1.Columns.Add("Start date", typeof(DateTime));
            dataTable1.Columns.Add("End date", typeof(DateTime));
            dataTable1.Columns.Add("Price", typeof(string));
            dataTable1.Columns.Add("Repeats", typeof(int));
            dataTable1.Columns.Add("For", typeof(int));
            //
            dataTable2.Columns.Add("Id", typeof(int));
            dataTable2.Columns.Add("Start Airport", typeof(string));
            dataTable2.Columns.Add("End Airport", typeof(string));
            comboBox1.Items.Clear();
            ServiceSingle servS = ServiceSingle.Instance;
            airlines = servS.getService().GetAll(typeof(InSafetyAirClient.InSafetyAirService.AirLine).Name).OfType<InSafetyAirClient.InSafetyAirService.AirLine>().ToList();
            foreach (InSafetyAirService.AirLine airline in airlines)
            {
                comboBox1.Items.Add(airline.name);
                autoComplete.Add(airline.name);
            }
            comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox1.AutoCompleteCustomSource = autoComplete;
            planes = servS.getService().GetAll(typeof(InSafetyAirClient.InSafetyAirService.Plane).Name).OfType<InSafetyAirClient.InSafetyAirService.Plane>().ToList();
            foreach (InSafetyAirService.Plane plane in planes)
            {
                comboBox2.Items.Add(plane.registrationNo);
                autoComplete.Add(plane.registrationNo);
            }
            comboBox2.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox2.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox2.AutoCompleteCustomSource = autoComplete;
            airports = servS.getService().GetAll(typeof(InSafetyAirClient.InSafetyAirService.Airport).Name).OfType<InSafetyAirClient.InSafetyAirService.Airport>().ToList();
            foreach (InSafetyAirService.Airport airport in airports)
            {
                comboBox3.Items.Add(airport.name);
                comboBox4.Items.Add(airport.name);
                autoComplete.Add(airport.name);
            }
            comboBox3.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox3.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox3.AutoCompleteCustomSource = autoComplete;
            comboBox4.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox4.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox4.AutoCompleteCustomSource = autoComplete;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            hus = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            hus = false;
        }

        private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
        {
            if (hus)
            {
                textBox2.Text = monthCalendar1.SelectionRange.Start.ToString();
                sDate = monthCalendar1.SelectionRange.Start;
            }
            else
            {
                textBox3.Text = monthCalendar1.SelectionRange.Start.ToString();
                eDate = monthCalendar1.SelectionRange.Start;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            sDate = sDate.AddHours(System.Convert.ToInt32(numericUpDown1.Value));
            sDate = sDate.AddMinutes(System.Convert.ToInt32(numericUpDown2.Value));
            eDate = sDate.AddHours(System.Convert.ToInt32(numericUpDown4.Value));
            eDate = sDate.AddMinutes(System.Convert.ToInt32(numericUpDown3.Value));
            if (sDate < eDate)
            {
                ServiceSingle servS = ServiceSingle.Instance;
                InSafetyAirService.Schedule sh= new InSafetyAirService.Schedule();
                sh.startTime = sDate;
                sh.endTime = eDate;
                sh.price = System.Convert.ToInt32(textBox4.Text);
                int id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
                sh.flightSegmentId = id;
                shedules.Add(sh);       
                TimeSpan dif =eDate-sDate;
                sh =(InSafetyAirService.Schedule) ServiceSingle.Instance.getService().AddToDatabase(sh);
                for (DateTime i = sh.startTime.AddDays((int)numericUpDown5.Value); i < sh.startTime.AddMonths((int)numericUpDown6.Value); i = i.AddDays((int)numericUpDown5.Value))
                {
                    InSafetyAirService.Schedule newShe = new InSafetyAirService.Schedule();
                    newShe.startTime = i;
                    newShe.endTime = i + dif;
                    newShe.flightSegmentId = id;
                    newShe.price = System.Convert.ToInt32(textBox4.Text);
                    ServiceSingle.Instance.getService().AddToDatabase(newShe);
                }
                dataTable1.Rows.Add(sh.scheduleId, sDate, eDate, textBox4.Text,numericUpDown5.Value, numericUpDown6.Value);
                dataGridView2.DataSource = dataTable1;

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            InSafetyAirService.FlightSegment fs=new InSafetyAirService.FlightSegment();
            foreach(InSafetyAirService.Airport air in airports)
            {
                if (air.name.Equals(comboBox3.Text))
                    fs.startAirportId = air.airportId;
                if (air.name.Equals(comboBox4.Text))
                    fs.endAirportId = air.airportId;
            }
            fs.flightId = f.flightId;
            fs = (InSafetyAirService.FlightSegment)ServiceSingle.Instance.getService().AddToDatabase(fs);
            segments.Add(fs);
            dataTable2.Rows.Add(fs.flightSegmentId,comboBox3.Text,comboBox4.Text);
            dataGridView1.DataSource = dataTable2;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            f= new InSafetyAirService.Flight();
            foreach (InSafetyAirService.AirLine air in airlines)
            {
                if(air.name.Equals(comboBox1.Text))
                    f.airLineId=air.airLineId;
            }
            foreach (InSafetyAirService.Plane plane in planes)
            {
                if(plane.registrationNo.Equals(comboBox2.Text))
                    f.planeId=plane.planeId;
            } 
            f.flightNumber=textBox1.Text;
            f=(InSafetyAirService.Flight) ServiceSingle.Instance.getService().AddToDatabase(f);
        }

        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if(dataGridView1.SelectedRows[0]!=null)
            {
                label13.Text = "Working on " + dataGridView1.SelectedRows[0].Cells[1].Value + " to " + dataGridView1.SelectedRows[0].Cells[2].Value;
            }
        }


    }
}
